<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="经销商" prop="partnersId">-->
<!--        <partners-select :key="mapComponentKey" v-model="queryParams.partnersId"></partners-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="经销商" prop="partnersName">-->
<!--        <el-input-->
<!--            v-model="queryParams.partnersName"-->
<!--            placeholder="请输入经销商名称"-->
<!--            clearable-->
<!--            @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="订单量">
        <el-row>
          <el-col :span="11">
            <el-input-number v-model="queryParams.miniOrderCount" :controls="false" :min="0" :precision="0"
                             placeholder="最小订单量"></el-input-number>
          </el-col>
          <el-col :span="2">-</el-col>
          <el-col :span="11">
            <el-input-number v-model="queryParams.maxOrderCount" :controls="false" :min="0" :precision="0"
                             placeholder="最大订单量"></el-input-number>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="营业额">
        <el-row>
          <el-col :span="11">
            <el-input-number v-model="queryParams.miniTurnover" :controls="false" :precision="2"
                             placeholder="最小营业额"></el-input-number>
          </el-col>
          <el-col :span="2">-</el-col>
          <el-col :span="11">
            <el-input-number v-model="queryParams.maxTurnover" :controls="false" :precision="2"
                             placeholder="最大营业额"></el-input-number>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="primary"-->
<!--            plain-->
<!--            icon="Plus"-->
<!--            @click="handleAdd"-->
<!--            v-hasPermi="['system:topencity:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:sysCityStatistics:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sysCityStatisticsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="城市" width="200" align="center" prop="extName"/>
<!--      <el-table-column label="开通时间" width="150" align="center" prop="contractTime"/>-->
<!--      <el-table-column label="状态" align="center" width="200" prop="status"/>-->
      <el-table-column label="营业额" align="center" width="200" prop="turnover"/>
      <el-table-column label="订单量" align="center" width="100" prop="orderCount"/>
      <el-table-column label="商家量" align="center" width="200" prop="merchantCount"/>
      <el-table-column label="活动数量" align="center" width="100" prop="activityCount"/>
<!--      <el-table-column label="经销商" align="center" width="200" prop="partnersName"/>-->
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="topencityRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="城市编码" prop="cityCode">
              <el-cascader-area v-model="form.cityCode" :qrtType="1"></el-cascader-area>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SysCityStatistics">
import {getStatisticsList} from "@/api/system/sysCity";
import {ref} from "vue";
import {addTopencity} from "@/api/system/topencity";

const {proxy} = getCurrentInstance();
const sysCityStatisticsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const mapComponentKey = ref(0)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    miniOrderCount: undefined,
    maxOrderCount: undefined,
    miniTurnover: undefined,
    maxTurnover: undefined,
    partnersId: undefined
  },
  rules: {
    cityCode: [
      {required: true, message: "城市编码不能为空", trigger: "change"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询城市统计列表 */
function getList() {
  loading.value = true;
  getStatisticsList(queryParams.value).then(response => {
    sysCityStatisticsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: undefined,
    name: undefined,
    partnersId: undefined,
    treeLevel: undefined,
    extId: undefined,
    enName: undefined,
    initial: undefined,
    extName: undefined,
    cityCode: undefined
  };
  proxy.resetForm("topencityRef");
}

function queryReset() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    miniOrderCount: undefined,
    maxOrderCount: undefined,
    miniTurnover: undefined,
    maxTurnover: undefined,
    partnersId: undefined
  };
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  mapComponentKey.value += 1;
  queryReset();
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/sysCity/getStatisticsListExport', {
    ...queryParams.value
  }, `getStatisticsListExport_${new Date().getTime()}.xlsx`)
}
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增开通城市";
}
function submitForm() {
  proxy.$refs["topencityRef"].validate(valid => {
    if (valid) {
      addTopencity(form.value).then(response => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
  });
}

onActivated(() => {
  getList();
})

getList();
</script>
